package com.snailcatmall.www.serversocket.netty.threadTools;

import com.snailcatmall.www.serversocket.pojo.ClientInfo;
import com.snailcatmall.www.serversocket.tools.GlobalStaticClass;
import io.netty.buffer.Unpooled;

import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.Map;

/**
 * @see NettyServerHeartbeatDetection 服务器不断像客户端发送消息作为服务器存在的证据
 */
public class NettyServerHeartbeatDetection implements Runnable{

    @Override
    public void run() {
        try {
            Thread.sleep(50);
            for (Map.Entry<SocketAddress, ClientInfo> entrySet: GlobalStaticClass.channelHandlerContextMap.entrySet()){
                entrySet.getValue().getChannelHandlerContext().writeAndFlush(Unpooled.copiedBuffer(ByteBuffer.allocateDirect(1)));
            }
        }catch (Exception e){e.printStackTrace();}
        run();
    }
}
